package com.heaboy.feedback.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.heaboy.feedback.mapper.CourseStudentMapper;
import com.heaboy.feedback.domain.CourseStudent;
import com.heaboy.feedback.service.ICourseStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
/**
 * 课程学生Service业务层处理
 * 
 * @author heaboy
 * @date 2024-02-01
 */
@Service
public class CourseStudentServiceImpl extends ServiceImpl<CourseStudentMapper, CourseStudent> implements ICourseStudentService
{
    @Autowired
    private CourseStudentMapper courseStudentMapper;

    /**
     * 查询课程学生
     * 
     * @param id 课程学生主键
     * @return 课程学生
     */
    @Override
    public CourseStudent selectCourseStudentById(Long id)
    {
        return courseStudentMapper.selectCourseStudentById(id);
    }

    /**
     * 查询课程学生列表
     * 
     * @param courseStudent 课程学生
     * @return 课程学生
     */
    @Override
    public List<CourseStudent> selectCourseStudentList(CourseStudent courseStudent)
    {
        return courseStudentMapper.selectCourseStudentList(courseStudent);
    }

    /**
     * 新增课程学生
     * 
     * @param courseStudent 课程学生
     * @return 结果
     */
    @Override
    public int insertCourseStudent(CourseStudent courseStudent)
    {
        courseStudent.setCreateTime(DateUtils.getNowDate());
        return courseStudentMapper.insertCourseStudent(courseStudent);
    }

    /**
     * 修改课程学生
     * 
     * @param courseStudent 课程学生
     * @return 结果
     */
    @Override
    public int updateCourseStudent(CourseStudent courseStudent)
    {
        courseStudent.setUpdateTime(DateUtils.getNowDate());
        return courseStudentMapper.updateCourseStudent(courseStudent);
    }

    /**
     * 批量删除课程学生
     * 
     * @param ids 需要删除的课程学生主键
     * @return 结果
     */
    @Override
    public int deleteCourseStudentByIds(Long[] ids)
    {
        return courseStudentMapper.deleteCourseStudentByIds(ids);
    }

    /**
     * 删除课程学生信息
     * 
     * @param id 课程学生主键
     * @return 结果
     */
    @Override
    public int deleteCourseStudentById(Long id)
    {
        return courseStudentMapper.deleteCourseStudentById(id);
    }

    @Override
    public boolean bindStudents(Long courseId, List<Long> studentIds) {
        List<CourseStudent> courseStudents = new ArrayList<>();
        for (Long studentId : studentIds) {
            CourseStudent courseStudent = new CourseStudent();
            courseStudent.setStudentId(studentId);
            courseStudent.setCourseId(courseId);
            courseStudents.add(courseStudent);
        }
        return saveBatch(courseStudents);
    }

    @Override
    public boolean unBindStudents(Long courseId, List<Long> studentIds) {
        return remove(new LambdaUpdateWrapper<CourseStudent>().eq(CourseStudent::getCourseId,courseId).in(CourseStudent::getStudentId,studentIds));
    }
}
